// These examples are from the Electronics Cookbook by Simon Monk
//https://github.com/LSatan/SmartRC-CC1101-Driver-Lib
// mod by Little_S@tan
#include <ELECHOUSE_CC1101_SRC_DRV.h>

int gdo0;//
#define Led1 PB14//定义led1引脚
#define Led2 PB15//定义led2引脚
#define Led3 PB5//定义led3引脚
#define Led4 PB6//定义led4引脚
#define Led5 PB7//定义led5引脚
#define Led6 PB8//定义led6引脚
#define Led7 PB9//定义led7引脚
#define Led8 PB10//定义led8引脚 


void setup(){

    gdo0=PA2;

    pinMode(Led1,OUTPUT);//设置led1引脚为输出模式
    pinMode(Led2,OUTPUT);//设置led2引脚为输出模式
    pinMode(Led3,OUTPUT);//设置led3引脚为输出模式
    pinMode(Led4,OUTPUT);//设置led4引脚为输出模式
    pinMode(Led5,OUTPUT);//设置led5引脚为输出模式
    pinMode(Led6,OUTPUT);//设置led6引脚为输出模式
    pinMode(Led7,OUTPUT);//设置led7引脚为输出模式
    pinMode(Led8,OUTPUT); //设置led8引脚为输出模式

    Serial.begin(9600);//初始化串口，波特率9600
    
    if (ELECHOUSE_cc1101.getCC1101()){         // Check the CC1101 Spi connection.
    Serial.println("Connection OK");
    }else{
    Serial.println("Connection Error");
    }
  
    ELECHOUSE_cc1101.Init();               // must be set to initialize the cc1101!
    ELECHOUSE_cc1101.setGDO0(gdo0);       // set lib internal gdo pin (gdo0). Gdo2 not use for this example.
    ELECHOUSE_cc1101.setCCMode(1);       // set config for internal transmission mode.
    ELECHOUSE_cc1101.setModulation(0);  // set modulation mode. 0 = 2-FSK, 1 = GFSK, 2 = ASK/OOK, 3 = 4-FSK, 4 = MSK.
    ELECHOUSE_cc1101.setMHZ(433.92);   // Here you can set your basic frequency. The lib calculates the frequency automatically (default = 433.92).The cc1101 can: 300-348 MHZ, 387-464MHZ and 779-928MHZ. Read More info from datasheet.
    ELECHOUSE_cc1101.setSyncMode(2);  // Combined sync-word qualifier mode. 0 = No preamble/sync. 1 = 16 sync word bits detected. 2 = 16/16 sync word bits detected. 3 = 30/32 sync word bits detected. 4 = No preamble/sync, carrier-sense above threshold. 5 = 15/16 + carrier-sense above threshold. 6 = 16/16 + carrier-sense above threshold. 7 = 30/32 + carrier-sense above threshold.
    ELECHOUSE_cc1101.setCrc(1);      // 1 = CRC calculation in TX and CRC check in RX enabled. 0 = CRC disabled for TX and RX.

    Serial.println("Rx Mode");
    //初始化全部关灯
    digitalWrite(Led1, 0);
    digitalWrite(Led2, 0);
    digitalWrite(Led3, 0);
    digitalWrite(Led4, 0);
    digitalWrite(Led5, 0);
    digitalWrite(Led6, 0);
    digitalWrite(Led7, 0);
    digitalWrite(Led8, 0);
}
byte buffer[100] = {0};//定义一个100字节的接收缓冲区

void loop()
{
  

    if (ELECHOUSE_cc1101.CheckReceiveFlag())//检查接收标志位
    {
    
    if (ELECHOUSE_cc1101.CheckCRC())//检查CRC,是否接收正确
    {    //CRC Check. If "setCrc(false)" crc returns always OK!
    // Serial.print("Rssi: ");
    // Serial.println(ELECHOUSE_cc1101.getRssi());
    // Serial.print("LQI: ");
    // Serial.println(ELECHOUSE_cc1101.getLqi());
    
    int len = ELECHOUSE_cc1101.ReceiveData(buffer);//接收数据并返回数据长度
    buffer[len] = '\0';//添加接收缓冲区的结束符
    
    int receivedValue = buffer[0]; // buffer[0] 存储接收到的值
    // Serial.println(buffer[len]);
    Serial.println((char *) buffer);//打印接收到的数据
    //判断接收的数字0-8执行相应的灯亮    
    if(buffer[0]=='1')
    { 
      digitalWrite(Led1,0);      
    }
    if(buffer[0]=='1')
    {
      digitalWrite(Led1,1);      
    }
    else if(buffer[0]=='2')
    {
      digitalWrite(Led1,1); 
      digitalWrite(Led2,1);     
    }
    else if(buffer[0]=='3')
    {
      digitalWrite(Led1,1); 
      digitalWrite(Led2,1);
      digitalWrite(Led3,1);     
    }
    else if(buffer[0]=='4')
    {
      digitalWrite(Led1,1); 
      digitalWrite(Led2,1);
      digitalWrite(Led3,1);  
      digitalWrite(Led4,1); 
    
    }
    else if(buffer[0]=='5')
    {
      digitalWrite(Led1,1); 
      digitalWrite(Led2,1);
      digitalWrite(Led3,1);  
      digitalWrite(Led4,1); 
      digitalWrite(Led5,1);          
    }
    else if(buffer[0]=='6')
    {
      digitalWrite(Led1,1); 
      digitalWrite(Led2,1);
      digitalWrite(Led3,1);  
      digitalWrite(Led4,1); 
      digitalWrite(Led5,1);  
      digitalWrite(Led6,1);    
    }
    else if(buffer[0]=='7')
    {
      digitalWrite(Led1,1); 
      digitalWrite(Led2,1);
      digitalWrite(Led3,1);  
      digitalWrite(Led4,1); 
      digitalWrite(Led5,1);  
      digitalWrite(Led6,1); 
      digitalWrite(Led7,1);    
    }
    else if(buffer[0]=='8')
    {
      digitalWrite(Led1,1); 
      digitalWrite(Led2,1);
      digitalWrite(Led3,1);  
      digitalWrite(Led4,1); 
      digitalWrite(Led5,1);  
      digitalWrite(Led6,1); 
      digitalWrite(Led7,1);  
      digitalWrite(Led8,1);    
    }
    else 
    {
      digitalWrite(Led1, 0);
      digitalWrite(Led2, 0);
      digitalWrite(Led3, 0);
      digitalWrite(Led4, 0);
      digitalWrite(Led5, 0);
      digitalWrite(Led6, 0);
      digitalWrite(Led7, 0);
      digitalWrite(Led8, 0);
    }
        /*
    for (int i = 0; i<len; i++){
    Serial.print(buffer[i]);
    Serial.print(",");
    }
    Serial.println(); */
    }
    }
}
